//
//  CollectionViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "CollectionViewController.h"
#import "ActivityDetailViewController.h"
#import "DataBaseSingleton.h"
#import "Activity.h"

@interface CollectionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *collectionTableView;

@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.collectionTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.collectionTableView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.collectionTableView.dataSource = self;
    self.collectionTableView.delegate = self;
    [self.view addSubview:self.collectionTableView];
    [self.collectionTableView release];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld", self.listArray.count);
    return self.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    cell.textLabel.text = [self.listArray objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailViewController *activityVC = [[ActivityDetailViewController alloc] init];
    NSString *title = [self.listArray objectAtIndex:indexPath.row];
    Activity *activity = [[Activity alloc] init];
    NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectActivityList:title];
    
    for (activity in array) {
        activityVC.act = activity;
    }
    [self.navigationController pushViewController:activityVC animated:YES];
    [activityVC release];
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [self.listArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationBottom];
        
        NSString *title = [self.listArray objectAtIndex:indexPath.row];
        NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
        
        [[DataBaseSingleton shareDataBase] deleteCollect:name :title];
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.collectionTableView setEditing:editing animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
